package com.example.forumsystem.demos.web.controller;

import com.example.forumsystem.demos.web.common.Constans;
import com.example.forumsystem.demos.web.common.ResultCode;
import com.example.forumsystem.demos.web.model.MessageInfo;
import com.example.forumsystem.demos.web.model.Result;
import com.example.forumsystem.demos.web.model.UserInfo;
import com.example.forumsystem.demos.web.service.IMessageService;
import com.example.forumsystem.demos.web.service.IUserService;
import io.micrometer.core.lang.NonNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api("测试站内信接口")
@RestController
@Slf4j
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IMessageService messageService;
    @ApiOperation("发送站内信")
    @RequestMapping("/sendMessage")
    public Result sendMessage (HttpServletRequest request,
                               @ApiParam("接收者Id") @RequestParam("receiveUserId") @NonNull Long receiveUserId,
                               @ApiParam("内容") @RequestParam("content") @NonNull String content) {
        HttpSession session = request.getSession(false);
        UserInfo user = (UserInfo) session.getAttribute(Constans.USER_SESSION);

        if (user.getState() == 1) {
            log.info("用户是禁言状态，无法发送消息");
            return Result.fail();
        }

        if (user.getId() == receiveUserId) {
            log.info("无法给自己发送站内信");
            return Result.fail();
        }

        UserInfo receiveUser = userService.selectById(receiveUserId);
        if (receiveUser == null || receiveUser.getDeleteState() == 1) {
            log.info("接收者不存在，或者是删除状态");
            return Result.fail();
        }

        MessageInfo message = new MessageInfo();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);

        messageService.create(message);
        return Result.success();
    }

    @ApiOperation("获取未读数")
    @RequestMapping("/getUnreadCount")
    public Integer getUnreadCount (HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        UserInfo user = (UserInfo) session.getAttribute(Constans.USER_SESSION);

        return messageService.selectUnreadCount(user.getId());
    }

    @ApiOperation("查询用户的所有站内信")
    @RequestMapping("/getAllMessage")
    public List<MessageInfo> getAllMessage (HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        UserInfo user = (UserInfo) session.getAttribute(Constans.USER_SESSION);

        return messageService.selectByReceiveUserId(user.getId());
    }

    @ApiOperation("更新为已读")
    @RequestMapping("/markRead")
    public void markRead (HttpServletRequest request,
                               @ApiParam("站内信Id") @RequestParam("id") @NonNull Long id) {
        MessageInfo message = messageService.selectById(id);
        if (message == null || message.getDeleteState() == 1) {
            log.info("站内信不存在或为删除状态");
            return;
        }

        messageService.updateStateById(id, (byte) 1);
    }

    @ApiOperation("回复站内信")
    @RequestMapping("/reply")
    public void reply (HttpServletRequest request,
                            @ApiParam("要回复的站内信Id") @RequestParam("repliedId") @NonNull Long repliedId,
                            @ApiParam("站内信的内容") @RequestParam("content") @NonNull String content) {
        HttpSession session = request.getSession(false);
        UserInfo user = (UserInfo) session.getAttribute(Constans.USER_SESSION);
        if (user.getState() == 1) {
            log.info("当前用户是禁言状态，无法回复");
        }

        MessageInfo existsMessage = messageService.selectById(repliedId);
        if (existsMessage == null || existsMessage.getDeleteState() == 1) {
            log.info("要回复的站内信不存在或者已经被删除了, 无法回复");
        }
        if (user.getId() == existsMessage.getPostUserId()) {
            log.info("不能回复自己的站内信");
        }

        MessageInfo message = new MessageInfo();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(existsMessage.getPostUserId());
        message.setContent(content); // 内容

        messageService.reply(repliedId, message);
    }
}
